import { Toast } from 'vant'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: "/myregister",
    name: 'myregister',
    meta: { title: '注册页' },
    component: () => import('../views/MyRegister.vue')
  },
  {
    path: "/mylogin",
    name: 'mylogin',
    meta: { title: '登录页' },
    component: () => import('../views/MyLogin.vue')
  },
  {
    path: "/myindex",
    name: 'myindex',
    meta: { title: '登录页' },
    component: () => import('../views/MyIndex.vue'),

    children: [
      {
        path: "/myindex/index",
        name: 'index',
        meta: { title: '首页' },
        component: () => import('../views/index/index.vue')
      },
      {
        path: "/myindex/wode",
        name: 'wode',
        meta: { title: '我的' },
        component: () => import('../views/index/wode.vue')
      },
      {
        path: "/myindex/dingdan",
        name: 'dingdan',
        meta: { title: '订单' },
        component: () => import('../views/index/dingdan.vue')
      },
      {
        path: "/myindex/baobaotuan",
        name: 'baobaotuan',
        meta: { title: '爆爆团' },
        component: () => import('../views/index/baobaotuan.vue')
      },
      {
        path: '/myindex',
        redirect: '/myindex/index'
      }
    ]
  },
  {
    path: '/',
    redirect: 'myregister'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由前置守卫
router.beforeEach((to, form, next) => {

  if (to.path == "/myindex/dingdan") {
    if (localStorage.getItem('token') && localStorage.getItem('userinfo')) {
      next();
    } else {
      Toast.fail('访问受限，请先登录')
      next('/mylogin');
    }
  } else {
    next();
  };


  if (to.path == "/myindex/wode") {
    if (localStorage.getItem('token') && localStorage.getItem('userinfo')) {
      next();
    } else {
      Toast.fail('访问受限，请先登录')
      next('/mylogin');
    }
  } else {
    next();
  };



})

// 路由后置守卫
router.afterEach((to, from) => {
  document.title = to.meta.title
})
export default router
